const Koa = require('koa');
const Router = require('koa-router');
const cors = require('@koa/cors');
const xmlParser = require('koa-xml-body');
const bodyParser = require('koa-bodyparser');
const helmet = require('koa-helmet');
const respond = require('koa-respond');
const logger = require('koa-logger');

const app = new Koa();
app.use(xmlParser());
const router = new Router();
const port = 3000;
/** middleware 中间件 */
let middleware = require('./middleware/index');
/** 公共函数 （污染全局的做法，但是这样很方便）*/
app.use(middleware.common());
app.use(middleware.author());
app.use(middleware.check());
/** 错误处理*/
app.use(middleware.errorHandle());
/**加载路由 */
require('./router')(router);
/**加载其他文件 */
app.use(cors())
    .use(logger())
    .use(bodyParser())
    .use(helmet())
    .use(respond())
    .use(router.routes())
    .use(router.allowedMethods())
    .listen(port, () => {
        console.log('The server is running at:');
        console.log(`    - Local:  http://localhost:${port}`);
    });
